﻿@{
    Layout = null;
}

<div>
    <div class="col-3 pr-0" style="display: none">
        <label id="INV-OutInventory-EntryInventoryId">@ViewData["EntryInventoryId"]</label>
        <label id="INV-OutInventory-OutInventoryId">@ViewData["OutInventoryId"]</label>
        <label id="INV-OutInventory-Parent-ModalID">@ViewData["ModalID"]</label>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">入库单编号</span>
        </div>
        <div class="col-9 pl-0">
            <input id="INV-OutInventory-EntryNumber" class="form-control font-rfs border-radius-noleft" disabled="disabled" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">物料代码</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="INV-OutInventory-MaterialCode" class="form-control font-rfs border-radius-noleft" style="width:45%" disabled="disabled" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">物料名称</span>
            <input id="INV-OutInventory-MaterialName" class="form-control font-rfs border-radius-noleft" style="width:32%" disabled="disabled" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">出库单编号</span>
        </div>
        <div class="col-9 pl-0">
            <input id="INV-OutInventory-InventoryNumber" class="form-control font-rfs border-radius-noleft" placeholder="可选" />

        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">可用数量</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="INV-OutInventory-EntryAvailableQuantity" class="form-control font-rfs border-radius-noleft" style="width:45%" disabled="disabled" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">单位</span>
            <input id="INV-OutInventory-EntryAvailableUnit" class="form-control font-rfs border-radius-noleft" style="width:32%" disabled="disabled" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">出库数量*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="INV-OutInventory-EntryQuantity" class="form-control font-rfs border-radius-noleft" style="width:45%" placeholder="必需是有效数字" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">单位</span>
            <input id="INV-OutInventory-EntryUnit" class="form-control font-rfs border-radius-noleft" style="width:32%" disabled="disabled" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">可消耗数量*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <input id="INV-OutInventory-AvailableQuantity" class="form-control font-rfs border-radius-noleft" placeholder="必需是有效数字" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">消耗单位*</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="INV-OutInventory-UnitsType1" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <select id="INV-OutInventory-Units1" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <button id="INV-OutInventory-AddUnits1" onclick="AddUnits(this)" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%" title="添加单位"><i class="bi-plus"></i></button>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">存储位置</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="INV-OutInventory-Select-Location" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">存储条件</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="INV-OutInventory-Select-EquipmentType" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">有效期至</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="INV-OutInventory-Vld" class="form-control font-rfs border-radius-noleft" style="width:45%" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">到期提醒</span>
            <input id="INV-OutInventory-Remind" class="form-control font-rfs border-radius-noleft" style="width:32%" placeholder="整数" onkeyup="value=value.replace(/[^\d]/g,'')" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">单价</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="INV-OutInventory-UnitPrice" class="form-control font-rfs border-radius-noleft" style="width:45%" value="0.00" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">总价</span>
            <input id="INV-OutInventory-TotalPrice" class="form-control font-rfs border-radius-noleft" style="width:32%" value="0.00" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">备注</span>
        </div>
        <div class="col-9 pl-0">
            <input id="INV-OutInventory-Note" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div id="workflow" style="display: none">
        <div class="input-group" style="padding-right: 15px;">
            <div class="col-12 pr-0">
                <span class="input-group-text font-rfs border-radius-noright" style="justify-content:center">请选择审批流程和审批人</span>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">审批流程*</span>
            </div>
            <div class="col-9 pl-0 input-group">
                <select id="INV-OutInventory-WorkFlow" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">审批人*</span>
            </div>
            <div class="col-9 pl-0 input-group">
                <select id="INV-OutInventory-User" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            </div>
        </div>
        <div class="input-group">
            <div class="col-3 pr-0">
                <span class="input-group-text font-rfs border-radius-noright noRightBorder">发起说明</span>
            </div>
            <div class="col-9 pl-0">
                <input id="INV-OutInventory-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
            </div>
        </div>
    </div>
    <div class="text-right">
        <button id="INV-OutInventory-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="INV-OutInventory-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>
<div class="modal" id="INV-OutInventory-AddUnits-Modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="INV-OutInventory-AddUnits-modalTitle" class="pt-3 pl-3"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
                <label id="ModalID" style="display:none">INV-OutInventory-AddUnits-Modal</label>
            </div>
            <div class="modal-body">
                <div id="INV-OutInventory-AddUnits-Embed"></div>
            </div>
        </div>
    </div>
</div>

<script>

    $(document).ready(function () {
        LoadUnitType();
        GetLocationList('INV-OutInventory-Select-Location', '库存位置');

        $('#INV-OutInventory-Vld').datetimepicker({
            timepicker: false,
            format: "Y/m/d"
        });
        $.datetimepicker.setLocale('zh');

        if (($("#INV-OutInventory-EntryInventoryId").html()).length > 0) {
            LoadEntryInventory();
        }
        if (($("#INV-OutInventory-OutInventoryId").html()).length > 0) {
            LoadOutInventory();
        }
    });

    function LoadUnitType() {
        $.post("\\Common\\GetUnitType", {
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#INV-OutInventory-UnitsType1").append(new Option(data[i].type));
            }
            $("#INV-OutInventory-UnitsType1").prepend(new Option("", ""));
            $("#INV-OutInventory-UnitsType1").val("");
        });
    }

    $("#INV-OutInventory-UnitsType").change(function () {
        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
            Type: $("#INV-OutInventory-UnitsType").find("option:selected").text()
        }, function (data) {
            $("#INV-OutInventory-Units").empty();
            for (var i = 0; i < data.length; i++) {
                $("#INV-OutInventory-Units").append(new Option(data[i].code));
            }
        });
    });

    $("#INV-OutInventory-UnitsType1").change(function () {
        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
            Type: $("#INV-OutInventory-UnitsType1").find("option:selected").text()
        }, function (data) {
            $("#INV-OutInventory-Units1").empty();
            for (var i = 0; i < data.length; i++) {
                $("#INV-OutInventory-Units1").append(new Option(data[i].code));
            }
        });
    });

    function AddUnits(obj) {
        $("#INV-OutInventory-AddUnits-modalTitle").text("添加单位");

        $("#INV-OutInventory-AddUnits-Modal").modal({
            backdrop: 'static',
            keyboard: false
        });

        $.post("/Common/AddUnit", function (datas) {
            $("#INV-OutInventory-AddUnits-Embed").html(datas);
        });
    }

    function LoadEntryInventory() {
        $.post("GetInventory", {
            InventorId: $("#INV-OutInventory-EntryInventoryId").html()
        }, function (data) {
            $("#INV-OutInventory-EntryNumber").val(data.inventoryNumber);
            $("#INV-OutInventory-MaterialCode").val(data.materialCode);
            $("#INV-OutInventory-MaterialName").val(data.materialName);
            $("#INV-OutInventory-EntryAvailableQuantity").val(data.receivedQuantity);
            $("#INV-OutInventory-EntryAvailableUnit").val(data.availableUnit);
            $("#INV-OutInventory-EntryUnit").val(data.availableUnit);
            $("#INV-OutInventory-Vld").val(data.vld == null ? null : data.vld.split('T')[0]);
            $("#INV-OutInventory-Remind").val(data.remind);
            $("#INV-OutInventory-UnitPrice").val(data.unitPrice);

            if (data.isReview == true) {
                $("#workflow").show();
                LoadWorkFlowByType('INV-OutInventory-WorkFlow', "审核");
                LoadUsers('INV-OutInventory-User');
            }
        });
    }

    function LoadOutInventory() {
        $.post("GetInventory", {
            InventorId: $("#INV-OutInventory-OutInventoryId").html()
        }, function (data) {
            $("#INV-OutInventory-InventoryNumber").val(data.inventoryNumber);
            $("#INV-OutInventory-EntryQuantity").val(data.entryQuantity);
            $("#INV-OutInventory-EntryQuantity").attr("disabled", "disabled");
            $("#INV-OutInventory-AvailableQuantity").val(data.availableQuantity);
            $("#INV-OutInventory-AvailableQuantity").attr("disabled", "disabled");
            $("#INV-OutInventory-Units1").append(new Option(data.availableUnit));
            $("#INV-OutInventory-UnitPrice").val(data.unitPrice);
            $("#INV-OutInventory-TotalPrice").val(data.totalPrice);
            $("#INV-OutInventory-Select-Location").val(data.positionId);
            $("#INV-OutInventory-Vld").val(data.vld == null ? null : data.vld.split('T')[0]);
            $("#INV-OutInventory-Remind").val(data.remind);
        });
    }

    $("#INV-OutInventory-confirm").click(function () {
        if ($.trim($("#INV-OutInventory-EntryQuantity").val()).length == 0) {
            alert("出库数量不能为空！");
            $("#INV-OutInventory-EntryQuantity").addClass('error-style');
            return false;
        }
        if ($.trim($("#INV-OutInventory-AvailableQuantity").val()).length == 0) {
            alert("可消耗数量不能为空！");
            $("#INV-OutInventory-AvailableQuantity").addClass('error-style');
            return false;
        }
        if ($.trim($("#INV-OutInventory-Units1").find("option:selected").text()).length == 0) {
            alert("可消耗单位必需选择！");
            $("#INV-OutInventory-Units1").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["MaterialCode"] = $.trim($("#INV-OutInventory-MaterialCode").val());
        jsonStr["MaterialName"] = $.trim($("#INV-OutInventory-MaterialName").val());
        jsonStr["InventoryNumber"] = $.trim($("#INV-OutInventory-InventoryNumber").val());
        jsonStr["EntryQuantity"] = $.trim($("#INV-OutInventory-EntryQuantity").val());
        jsonStr["EntryUnit"] = $.trim($("#INV-OutInventory-EntryUnit").val());
        jsonStr["AvailableQuantity"] = $.trim($("#INV-OutInventory-AvailableQuantity").val());
        jsonStr["AvailableUnit"] = $.trim($("#INV-OutInventory-Units1").find("option:selected").text());
        jsonStr["Vld"] = $('#INV-OutInventory-Vld').val();
        jsonStr["Remind"] = $('#INV-OutInventory-Remind').val();
        jsonStr["Note"] = $.trim($("#INV-OutInventory-Note").val());
        jsonStr["UnitPrice"] = $('#INV-OutInventory-UnitPrice').val();
        jsonStr["TotalPrice"] = $('#INV-OutInventory-TotalPrice').val();
        jsonStr["PositionId"] = $("#INV-OutInventory-Select-Location").find("option:selected").text();

        var EntryInventoryId = $("#INV-OutInventory-EntryInventoryId").html();
        var OutInventoryId = $("#INV-OutInventory-OutInventoryId").html();
        if (OutInventoryId == "") {
            if (!$("#workflow").is(':hidden')) {
                AddInventoryWF(jsonStr);
            } else {
                $.post("postAddOutInventory", {
                    EntryInventorId: $("#INV-OutInventory-EntryInventoryId").html(),
                    jsonStr: JSON.stringify(jsonStr)
                }, function (data) {
                    if (data.indexOf("成功") >= 0) {
                        $("#INV-OutInventory-cancel").click();
                    } else {
                        alert(data);
                    }
                });
            }
        } else {
            jsonStr["Id"] = $("#INV-OutInventory-OutInventoryId").html();
            $.post("UpdateOutInventory", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#INV-OutInventory-cancel").click();
                } else {
                    alert(data);
                }
            });
        }
    });

    $("#INV-OutInventory-cancel").click(function () {
        var modalID = $("#INV-OutInventory-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

    function AddInventoryWF(jsonStr) {
        if ($("#INV-OutInventory-WorkFlow").val().length == 0) {
            alert("必需选择要发起的流程！");
            $("#INV-OutInventory-WorkFlow").addClass('error-style');
            return false;
        }
        if ($("#INV-OutInventory-User").val().length == 0) {
            alert("必需选择一个审批人！");
            $("#INV-OutInventory-User").addClass('error-style');
            return false;
        }

        var StarWFParameters = {};
        StarWFParameters["WorkFlowId"] = $("#INV-OutInventory-WorkFlow").find("option:selected").val();
        StarWFParameters["DomainName"] = "Inventory";
        StarWFParameters["DomainId"] = ''; /* 这个 DomainId 是在后台获得的 */
        StarWFParameters["ApprovedByID"] = $("#INV-OutInventory-User").find("option:selected").val();
        StarWFParameters["ApprovedByName"] = $("#INV-OutInventory-User").find("option:selected").text();

        $.post("postAddOutInventory", {
            EntryInventorId: $("#INV-OutInventory-EntryInventoryId").html(),
            jsonStr: JSON.stringify(jsonStr),
            StarWFParameters: JSON.stringify(StarWFParameters)
        }, function (data) {
            if (data.indexOf("成功") >= 0) {
                $("#INV-OutInventory-cancel").click();
            } else {
                alert(data);
            }
        });
    }

</script>
